package com.example.github.io.stream.sequence;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

/**
 * 序列输入流
 */
public class SequenceInputStreamExample {
    public static void main(String[] args) throws IOException {
        /*
        // 1. 先创建两个FileInputStream 类的实例对象
        // 2. 将两个实例对象作为参数传给序列输入流
        // 3. 读取数据流信息
        // 4. 从里到外关闭流实例对象
        FileInputStream fis1 = new FileInputStream("D:/logs/test1.txt");
        FileInputStream fis2 = new FileInputStream("D:/logs/test2.txt");
        SequenceInputStream stream = new SequenceInputStream(fis1, fis2);
        int i;
        while ((i=stream.read())!= -1) {
            System.out.print((char) i);
        }
        stream.close();
        fis2.close();
        fis1.close();
        System.out.println();
        System.out.println("success ...");
         */
        /**
         * 读取两个文件写入第三个文件 test1.txt test2.txt 写入test3.txt
         */
        FileInputStream fis1 = new FileInputStream("D:/logs/test1.txt");
        FileInputStream fis2 = new FileInputStream("D:/logs/test2.txt");
        Vector<Object> vector = new Vector<>();
        vector.add(fis1);
        vector.add(fis2);
        Enumeration elements = vector.elements();
        SequenceInputStream stream = new SequenceInputStream(elements);
        int i;
        String value = "";
        while((i = stream.read()) != -1){
            value += ((char)i);
            System.out.print((char)i);
        }
        System.out.println();
        System.out.println("str is " + value);
        // 将读取到的数据写入test3.txt

        FileOutputStream fis3 = new FileOutputStream("D:/logs/test3.txt");
        fis3.write(value.getBytes());
        fis3.close();
        stream.close();
        fis2.close();
        fis1.close();
    }
}
